package tcm.test.service.impl;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tcm.test.mapper.ProductMapper;
import tcm.test.pojo.dto.ProductDTO;
import tcm.test.pojo.entity.PageResult;
import tcm.test.pojo.entity.Product;
import tcm.test.service.ProductService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper ProductMapper;

    //添加新商品
    public void register(ProductDTO ProductDTO) {
        Product P = new Product();
        BeanUtils.copyProperties(ProductDTO, P);
        P.setCreateTime(LocalDateTime.now());
        P.setUpdateTime(LocalDateTime.now());
        ProductMapper.register(P);
    }

    //分页查询所有商品
    public  PageResult getALL(Integer page, Integer size) {

        Integer start = (page - 1) * size;
        List<Product> p= ProductMapper.getALL(start,size);

        PageResult pr = new PageResult();
        pr.setTotal(ProductMapper.count());

        pr.setRecords(p);
        return pr;
    }

    //根据id查询所有商品
    public Product getById(Long id) {
        return ProductMapper.getById(id);
    }

    //根据id更新商品
    public void update(ProductDTO productDTO) {
        Product p=new Product();
        BeanUtils.copyProperties(productDTO,p);
        p.setUpdateTime(LocalDateTime.now());
        ProductMapper.update(p);
    }

    //根据id删除商品
    public void deleteById(Long id) {
        ProductMapper.deleteById(id);
    }
}
